package com.wk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wk.dao.CmfzAdminDao;
import com.wk.dao.CmfzAdminRoleDao;
import com.wk.dao.CmfzRoleDao;
import com.wk.entity.CmfzAdmin;
import com.wk.entity.CmfzAdminRole;
import com.wk.entity.CmfzRole;
import com.wk.service.CmfzAdminRoleService;
import com.wk.service.CmfzAdminService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.management.Agent;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@Slf4j
public class CmfzAdminServiceImpl implements CmfzAdminService {

    @Autowired
    private CmfzAdminDao cmfzAdminDao;

    @Autowired
    private CmfzAdminRoleDao cmfzAdminRoleDao;

    @Autowired
    private CmfzRoleDao cmfzRoleDao;

    @Override
    public Map login(CmfzAdmin cmfzAdmin) {
        Map map = new HashMap();

        //创建一个查询条件构造器,
        QueryWrapper<CmfzAdmin> wrapper = new QueryWrapper<>();

        //封装条件
        wrapper.eq("username",cmfzAdmin.getUsername()).eq("password",cmfzAdmin.getPassword());

        CmfzAdmin login = cmfzAdminDao.selectOne(wrapper);

        if (login!=null){
            map.put("status",1);
            map.put("data","登陆成功");
        }else{
            map.put("status",2);
            map.put("data","用户名或密码错误");
        }
        return map;
    }


    @Override
    public Map remove(Integer id) {
        Map map = new HashMap();
        int i = cmfzAdminDao.deleteById(id);
        if(i>0){
            map.put("data","删除成功");
        }else{
            map.put("data","删除失败");
        }
        return map;
    }

    @Override
    public Map add(CmfzAdminRole cmfzAdminRole) {
        log.info("cmfzAdminRole1"+cmfzAdminRole);
        Map map = new HashMap();
        CmfzAdmin cmfzAdmin = cmfzAdminRole.getCmfzAdmin();
        CmfzRole cmfzRole = cmfzAdminRole.getCmfzRole();

        String password = cmfzAdmin.getPassword();

        for (int i = 0; i < 1024; i++) {
            password = DigestUtils.md5Hex(password);
        }
        cmfzAdmin.setPassword(password);
        try{
            cmfzAdminDao.insertSelective(cmfzAdmin);
            log.info("cmfzRole"+cmfzRole);
            log.info("cmfzAdmin"+cmfzAdmin);

            cmfzAdminRoleDao.insertAdminRole(cmfzAdminRole);
            map.put("data","添加成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("data","添加失败");
        }
        return map;
    }

    @Override
    public Map findOne(Integer id) {
        Map map = new HashMap();

        CmfzAdmin cmfzAdmin = cmfzAdminDao.selectById(id);
        CmfzAdminRole cmfzAdminRole = cmfzAdminRoleDao.findOne(id);
//        log.info("用户角色"+cmfzAdminRole);
        CmfzRole cmfzRole = cmfzAdminRole.getCmfzRole();
        List<CmfzRole> cmfzRoles = cmfzRoleDao.selectList(null);
        map.put("data",cmfzAdmin);
        map.put("roles",cmfzRoles);
        map.put("role",cmfzRole);

        return map;
    }

    @Override
    public Map update(CmfzAdmin cmfzAdmin,CmfzAdminRole cmfzAdminRole) {

        Map map = new HashMap();
        try {
            log.info("接收到的用户"+cmfzAdmin);
            log.info("接收到的用户角色"+cmfzAdminRole);
            CmfzRole cmfzRole = cmfzAdminRole.getCmfzRole();
            CmfzAdminRole admin_id = cmfzAdminRoleDao.selectOne(new QueryWrapper<CmfzAdminRole>().eq("admin_id", cmfzAdmin.getId()));
            log.info("查询到的角色用户"+admin_id);
            cmfzAdminDao.updateById(cmfzAdmin);

            cmfzAdminRole.setAdminId(cmfzAdmin.getId());
            cmfzAdminRole.setAdminRoleId(admin_id.getAdminRoleId());
            cmfzAdminRole.setRoleId(cmfzRole.getRoleId());
            cmfzAdminRoleDao.update(cmfzAdminRole);
            map.put("data","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("date","修改失败");
        }

        return map;
    }

    @Override
    public Map findByPage(Integer page, Integer limit) {
        Map map = new HashMap();
//
//        Page<CmfzAdmin> cmfzPage = new Page<CmfzAdmin>(page,limit);
//        Page page1 = cmfzAdminDao.selectPage(cmfzPage, null);
//        long total = page1.getTotal();
//        List<CmfzAdmin> records = page1.getRecords();
//
//        System.out.println("long"+total);
//
//        for (CmfzAdmin record : records) {
//            //根据管理员id查询角色
//            QueryWrapper<CmfzRole> wrapper = new QueryWrapper<CmfzRole>();
//            List<CmfzRole> role =  cmfzRoleDao.selectList(wrapper.eq("admin_id",record.getId()));
//        }


//        map.put("data",records);
//        map.put("count",total);
//        map.put("msg","");
//        map.put("code",0);
        page = (page-1)*limit;

        List all = cmfzAdminRoleDao.findAll(page, limit);
        Integer count = cmfzAdminRoleDao.findCount();
        log.info("查询到的角色用户"+all);

        map.put("data",all);
        map.put("count",count);
        map.put("msg","");
        map.put("code",0);

        return map;
    }

    @Override
    public void addAdmin(String username, String password) {
        UUID uuid = UUID.randomUUID();
        //生成盐
        String salt = uuid.toString().substring(0, 4);
        System.out.println("盐"+salt);

        Md5Hash md5Hash = new Md5Hash(password, salt, 1024);
        String pwd = md5Hash.toHex();

        CmfzAdmin cmfzAdmin = new CmfzAdmin();
        cmfzAdmin.setPassword(pwd);
        cmfzAdmin.setUsername(username);
        cmfzAdmin.setSalt(salt);

        cmfzAdminDao.insert(cmfzAdmin);

    }
}
